﻿/**
 
 ***/
var Common={
    seo_css:"http://localhost:92/css/widget/",
    target_url:"http://localhost:92",
    logo_default:"http://localhost:92/images/advertising.jpg",

    merge_options:function(n,t){
        for(var i in t)
            n[i]=t[i]; 
        return n
    },

    createIframe:function(n){
        var r={cat:"iframe",
                id:"",
                "class":"",
                height:0,
                name:"",
                width:0,
                frameborder:0,
                src:!1,
                style:"display: none !important",
                mainTag:"body"}, i=this.merge_options(r,n), t;
                if(i.src != !1){
                    t=document.createElement(i.cat);
                    switch(i.cat){
                        case"iframe":t.setAttribute("height",i.height),
                            t.setAttribute("width",i.width),
                            t.setAttribute("frameborder",i.frameborder),
                            t.setAttribute("style",i.style),
                            t.setAttribute("src",i.src);
                            break;
                        case"script":t.setAttribute("type","text/javascript"),
                            t.setAttribute("src",i.src);break;
                        case"link":t.setAttribute("rel","stylesheet"),
                            t.setAttribute("type","text/css"),
                            t.setAttribute("href",i.src)
                    }
                    document.getElementsByTagName(i.mainTag)[0].appendChild(t)
                }
    },
    msg:function(n){
        console.log(n)
    },
    addCss:function(n){
        var r="widget", i;
        typeof n!="undefined"&&(r="widget_"+n),
        i=Common.seo_css+r+'.css',
        this.createIframe({cat:"link",src:i,mainTag:"head"})
    }
};
var seo_lib = {
    ref: null,
    
    init_ref: function() {
        if (!seo_lib.ref) {
            try {
                if (window != window.top)
                    seo_lib.ref = "";//document.referrer;
                else
                    seo_lib.ref = "";//"location.href;
            } catch (error) {
                seo_lib.ref = location.href;
            }
        }
    },
    add_css : function(size_id){
        Common.addCss(size_id);
    },
    // Ham chinh thuc hien viec phan tich du lieu de hien thi
    bind_json: function(data) {
        // Kiem tra phien ban cua trinh duyet xem co hien thi duoc khong?
        if (window.navigator.userAgent.indexOf('MSIE 8') > 0 || window.navigator.userAgent.indexOf('MSIE 7') > 0 || window.navigator.userAgent.indexOf('MSIE 6') > 0) {
            if (!data) return;
            if (!data.is_loaded) {
                jQuery(document).ready(function() {
					data.is_loaded = true;
					seo_lib.bind_json(data);
				});
				return;
            }
        }
        if (!data || !data.widget) return;
        // Vẽ các widget tương ứng 1-product
        switch (data.widget.data_type) {
            case 1:
                this.bind_json_product(data);
                break;
            default:
                break;
        }
        var widget_html = jQuery('#seo_widget_' + data.widget.id);
        if (!widget_html) return;
        var widget_type = widget_html.attr('type');
        var ws = this.ws(data.widget.size_id);
        data = '';
    },
    bind_json_product: function(data) {
        if (!data || !data.items) return;
        var widget = data.widget;
        var widget_html = jQuery('#seo_widget_' + widget.id);
        var ws = this.ws(widget.size_id);
		if (ws.h <= 0 || ws.w <= 0) return;
        var i = 0, html = '', item, max_item = 0, len;
        var widget_style = {
            title_class: 'small',
            title_top: false,
            p_img_class: 'img40',
            allow_download: true,
            allow_des: true,
        };
        switch (widget.size_id) {
            case 4: /* 320x50 */
                this.add_css('320x50');
                widget_style.p_img_class = 'img40 fl';
                max_item = 1;
                break;
        }
        html = seo_lib.get_widget_html(seo_lib, widget_html, ws, widget, max_item, data, widget_style);
        widget_html.find('.widgetCont').html(html);
        widget_html.fadeIn();
        widget_html;
    },
    ws: function(id) {
        var size = {
            w: 0,
            h: 0
        };
        switch (id) {
            case 4:
                size = {
                    w: 320,
                    h: 50,
                    template: '<div class="widgetHoz"><div class="headingWidget"><div class="bg-seo"><div class="textHead"><a href="http://s.qplay.vn" target="_blank" class="QCVer">&nbsp;</a></div></div></div><div class="widgetCont"><span class="symbolB"></span> <span class="symbolR"></span> </div></div>'
                };
                break;
            default:
                break;
        }
        return size;
    },
    alert: function(mes) {
        alert(mes);
    },
    get_widget_html: function(seo_lib, widget_html, ws, widget, max_item, data, widget_style) {
        var i = 0, len = 0, html = '', item = '';
        var html_return = {item_title: '', p_img: '', p_hind: '', p_para: '', p_price: ''};
        widget_html.html(ws.template);
        widget_html = jQuery('#seo_widget_' + widget.id);
        len = data.items.length;
        //  them phan tu cho du so luong max (hien thi du khung)
        for (i = len; i < max_item; i++) {
                     data.items.push({
                         id: 0,
                         target_url: Common.target_url,
                         description: 'Quảng cáo trên di động',
                         title: 'Liên hệ: +84-4-3576 6085',
                         img_url: Common.logo_default
                     });
                 }
         
        len = data.items.length;
        
        for (i = 0; i < len && i < max_item; i++) {
            
            item = data.items[i];
            html_return.item_title = '<div class="' + widget_style.title_class + '"><a target="_blank" style="font-weight: bold !important;" seo="item_link" _id="' + item.id + '" href="' + item.target_url + '">' + item.title + '</a></div>';
            html_return.p_img = '<div class="' + widget_style.p_img_class + '"><a target="_blank" seo="item_link" _id="' + item.id + '" href="' + item.target_url + '"><img class="seo_item_img" src="' + item.img_url + '" alt="" /></a></div>';
            html_return.p_des = widget_style.allow_des ? '<div class="des">' + '<a target="_blank" seo="item_link" _id="' + item.id + '" href="' + item.target_url + '">' + item.description + '</a></div>' : '';
            html_return.p_download = widget_style.allow_download ? '<div class="download">' + '<a target="_blank" seo="item_link" _id="' + item.id + '" href="' + item.target_url + '">Click đây</a>' + '</div>' : '';
            // Phan tu cuoi khong can co vien duoi
            if (i == (max_item - 1))
                html += '<li class="no-bdr">';
            else
                html += '<li>';

            if (widget_style.title_top) {
                html += html_return.item_title;
                html += html_return.p_img;
            } else {
                html += html_return.p_img;
                html += html_return.item_title;
            }
            html += html_return.p_des;
            html += html_return.p_download;
            html += '</li>';
        }
        html = '<ul class="itemNews">' + html + '</ul>';
        return html;
    }
};